﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2010
// admin@franknagl.de
//
namespace SBIP.Filter
{
    using System;

    /// <summary>
    /// Threshold binarization.
    /// </summary>
    /// 
    /// <remarks>
    /// <para>
    /// The filter does image binarization using specified threshold value. All pixels (or 
    /// one channel of them) with intensities higher than threshold value are converted to white 
    /// pixels. All other pixels with intensities equal or below threshold value are converted to black pixels.
    /// </para>
    /// 
    /// <para>Sample usage:</para>
    /// <code>
    /// Bitmap image = new Bitmap( "cathedral.jpg" );
    ///
    /// // 1. Offline rendering of images
    /// // create Processor, used as rendering framework
    /// Processor processor = new Processor( );
    /// // starts Processor
    /// processor.Begin( image );
    /// // create Threshold filter
    /// Threshold filter = new Threshold( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    /// filter.Value = 150;
    /// // apply the filter, render to bitmap  
    /// Bitmap resultImage = processor.RenderToBitmap( );
    /// resultImage.Save("Threshold.jpg", ImageFormat.Jpeg);
    /// // apply the filter, render to texture
    /// // Texture2D resultTexture = processor.RenderToTexture( );
    /// processor.End( );
    ///  
    /// // 2. Online rendering of videos or images
    /// // create any windows control for rendering in
    /// Form myForm = new Form
    /// {
    ///     Width = 1280,
    ///     Height = 720
    /// };
    /// // starts Processor
    /// processor.Begin( "Video.avi", myForm );
    /// // create Threshold filter
    /// filter = new Threshold( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    /// filter.Value = 180;
    /// // apply the filter
    /// myForm.Show( );
    /// while ( myForm.Created )
    /// {
    ///     processor.Render( );
    ///     Application.DoEvents( );
    /// }
    /// processor.End( );
    /// </code>
    /// 
    /// <para><b>Initial image:</b></para>
    /// <img src="../../Cathedral.jpg" width="500" height="338" />
    /// <para><b>Result image (Threshold = 150):</b></para>
    /// <img src="../../Threshold.jpg" width="500" height="338" />
    /// </remarks>
    public sealed class Threshold : BaseFilter
    {
        private byte value;
        /// <summary>Threshold value.</summary>
        /// <remarks><para>Default value is set to 100.</para></remarks>
        public byte Value
        {
            get { return value; }
            set 
            {
                if (Effect == null)
                    throw new ArgumentException("Value" + InitMsg);

                this.value = value;
                Effect.Parameters["Value"].SetValue(value);
            }
        }      
        
        /// <summary>
        /// Initializes a new instance of the <see cref="Threshold"/> class.
        /// </summary>
        public Threshold()
            : base("Threshold") { }

        /// <summary>
        /// Initializes threshold value.
        /// </summary>
        protected override void PostInit()
        {
            Value = 100;
        }
    }
}
